









<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>JUnice - Guice Integration</title>
    <link rel="stylesheet" href="./css/maven-theme.css" type="text/css" />
    <link rel="stylesheet" href="./css/maven-base.css" type="text/css"/>
    <link rel="stylesheet" href="./css/prettify.css" type="text/css"/>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
    <script type="text/javascript" src="./js/prettify.js"></script>
    <script type="text/javascript" src="./js/niftycube.js"></script>
    <script type="text/javascript">
        window.onload=function() {
            Nifty("div\#wrapper", "big");
            Nifty("div#breadcrumbs", "small");
            Nifty("div\#navcolumn", "small");
            Nifty("div.source", "medium");
            prettyPrint();
        }
    </script>
          <meta name="author" content="Marco Speranza and Simone Tripodi" />
          </head>
  <body>
    <div id="wrapper">
        <div id="banner">
                        <div id="bannerLeft">
    
            JUnice
    
            </div>
                            <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="breadcrumbs">
              
  

  
    
            
  
    
            <div class="xleft">
        <span id="publishDate">Last Published: 2010-03-24</span>
                  &nbsp;| <span id="projectVersion">Version: 1.1</span>
                      </div>
                <div class="xright">      
  

  
    
            
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
        <div id="leftColumn">
          <div id="navcolumn">
               
  

  
    
            
  
    
                       <h5>JUnice</h5>
            <ul>
              
    <li class="none">
                    <a href="index.html">Home</a>
          </li>
              
    <li class="none">
                    <a href="core.html">The core - JUniceRunner</a>
          </li>
              
    <li class="none">
              <strong>Guice integration</strong>
        </li>
              
    <li class="none">
                    <a href="mock.html">Mock Framework integration</a>
          </li>
              
    <li class="none">
                    <a href="JUnice-1.1.pdf">PDF User Manual</a>
          </li>
          </ul>
              <h5>Project Documentation</h5>
            <ul>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-info.html">Project Information</a>
                </li>
              
                
                    
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
                  
              
        <li class="collapsed">
                    <a href="project-reports.html">Project Reports</a>
                </li>
          </ul>
              <h5>External Links</h5>
            <ul>
              
    <li class="none">
                    <a href="">Project Page</a>
          </li>
              
    <li class="none">
                    <a href="../google-guice/">Google Guice</a>
          </li>
              
    <li class="none">
                    <a href="http://www.junit.org" class="externalLink">JUnit</a>
          </li>
              
    <li class="none">
                    <a href="http://easymock.org/" class="externalLink">Easy-Mock</a>
          </li>
              
    <li class="none">
                    <a href="http://mockito.org/" class="externalLink">Mockito</a>
          </li>
          </ul>
                                               <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img class="poweredBy"
                 alt="Built by Maven"
                 src="./images/logos/maven-feather.png"
                 width="90"
                 height="30"
                 border="1" />
          </a>
                           
  

  
    
            
  
    
            </div>
        </div>
        <div id="bodyColumn">
          <div id="contentBox">
            <div class="section"><h2>Guice integration!</h2>
<p><i>JUnice</i> provides a couple of annotations to allow user reusing google-guice modules in test classes.</p>
<p><tt>@com.junice.annotation.GuiceModule</tt> is a class annotation usefull to indicate a list of google-guice modules class.</p>
<p><tt>@com.junice.annotation.GuiceProvidedModules</tt> is a method annotation usefull to indicate a provider method to create a custom google-guice module.</p>
<div class="section"><h3>GuiceModule annotation</h3>
<p>This example shows a typical use for this annotation; given the following module:</p>
<div class="source"><pre>public class SimpleModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(Hello.class).to(HelloWorld.class);
    }

}</pre>
</div>
<p>then users can reuse it specifying </p>
<div class="source"><pre>@RunWith(JUniceRunner.class)
@GuiceModules(modules={ SimpleModule.class,
                        AnotherAcmeModule.class })
public class SimpleTest {

 @com.google.inject.Inject
  private Hello helloWorld;
        
  @org.junit.Test
  public void testInjectNotStatic() {
    assertNotNull(helloWorld);
    assertEquals(&quot;Hello World!!!!&quot;, helloWorld.sayHallo());
  }

}</pre>
</div>
</div>
<div class="section"><h3>GuiceProvidedModule annotation</h3>
<p>GuiceProvidedModule is usefull when your test needs a module that not have a default costructor. So in you testcase you have to declare a <b>public static</b> method with the return type is <i>com.google.inject.Module</i> or <i>Iterable&lt;com.google.inject.Module&gt;</i> or or <i>com.google.inject.Module[]</i></p>
<div class="source"><pre>public class ComplexModule extends AbstractModule {

    private String name;

    public ComplexModule(String name) {
        this.name = name;
    }

    @Override
    protected void configure() {
        bind(WhoIm.class).toInstance(new WhoIm(name));
    }

}</pre>
</div>
<div class="source"><pre>@RunWith(JUniceRunner.class)
public class SimpleTest {

    @GuiceProvidedModules
    public static Module createComplexModule(){
        return new ComplexModule(&quot;Marco Speranza&quot;);
    }

    @com.google.inject.Inject
    public static WhoIm whoIm;

    @org.junit.Test
    public void testWhoIm() {
        assertNotNull(whoIm);
        assertEquals(&quot;Marco Speranza&quot;, whoIm.sayWhoIm());
    }

}</pre>
</div>
<p>Finally if you want create a module on the fly, your test case should extend <tt>com.google.inject.AbstractModule</tt> or implement a <tt>com.google.inject.Module</tt></p>
<div class="source"><pre>@RunWith(JUniceRunner.class)
public class SimpleTest extends AbstractModule {

    @Override
     public void configure() {
        bind(Integer.class)
             .annotatedWith(Names.named(&quot;version&quot;))
                    .toInstance(10);
    }

    @Inject
    @Named(&quot;version&quot;)
    private Integer version;

    @Test
    public void testInjectModuleClass(){
        assertNotNull(version);
        assertEquals(10, version.intValue());
    }

}</pre>
</div>
</div>
</div>

          </div>
        </div>
        <div class="clear">
          <hr/>
        </div>
        <div id="footer">
          <div class="xright">Copyright &#169;         
   
          2010.
    
      All Rights Reserved.    
  

  
    
            
  
    
  </div>
          <div class="clear">
            <hr/>
          </div>
        </div>
    </div>
  </body>
</html>
